package com.itcast.regi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.regi.dto.DishDto;
import com.itcast.regi.entity.Dish;
import com.itcast.regi.entity.DishFlavor;
import com.itcast.regi.mapper.DishFlavorMapper;
import com.itcast.regi.mapper.DishMapper;
import com.itcast.regi.service.DishFlavorService;
import com.itcast.regi.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishMapper dishMapper;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到dish表中
        this.save(dishDto);

        //获取菜品id
        Long dishId = dishDto.getId();

        //将菜品id加到集合中
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品的基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询菜品的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据——dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据——dish——flavor表中的insert操作

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());


        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 修改菜品是否停售
     * @param ids
     */
    @Override
    public void updateStatu(int status,List<Long> ids) {
        for(Long s : ids){
            Dish dish = dishMapper.selectById(s);
            if(dish.getStatus() ==1){
                dish.setStatus(status);
            }else if(dish.getStatus() ==0){
                dish.setStatus(status);
            }
            dishMapper.updateById(dish);
        }


    }


    @Transactional
    public void removeDishAll(String ids) {
        //mybatis方法删除
        //将前端传入的字符串拆分成一个一个的long型id
        String[] idAll = ids.split(",");

        for (int i = 0; i < idAll.length; i++) {
            Long id =  Long.parseLong(idAll[i]);

            List<Long> flavors = dishFlavorMapper.selectFlavor(id);

            for(Long s:flavors){
                dishFlavorMapper.deleteById(s);
            }

            dishMapper.deleteById(id);

//            mybatis-plus方法删除
//        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(DishFlavor::getDishId,id);
//        dishFlavorService.remove(queryWrapper);
//        this.removeById(id);
        }

    }


}
